<?php

/**
 * YTaxonomyListViewWidget
 *
 * @author Hua yang <htmlcook@gmail.com>
 */
class YTaxonomyListViewWidget extends CWidget
{
	/**
	 * 模型
	 *
	 * @var Node
	 */
	public $model;

	/**
	 * 运行
	 *
	 * @see CWidget::run()
	 */
	public function run()
	{
		if (!isset($this->model)) {
			return;
		}

		$terms = $this->model->getTerms();
		echo '<div class="taxonomies">';
		foreach ($this->model->prepareTaxonomies() as $taxonomy => $params) {
			echo '<dl><dt>' . CHtml::encode($params['label']) . '</dt>';
			if (isset($terms[$taxonomy])) {
				$termNames = array();
				if (is_array($terms[$taxonomy])) {
					foreach ($terms[$taxonomy] as $term) {
						$termNames[] = $term->name;
					}
				} else {
					$termNames[] = $terms[$taxonomy]->name;
				}

				echo '<dd>' . implode('</dd> <dd>', array_map(array('CHtml', 'encode'), $termNames)) . '</dd>';
			}
			echo '</dl>';
		}
		echo '</div>';
	}
}